package com.ciban.smarthome.module.ui;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.AppUtils;
import com.ciban.smarthome.common.utils.logcrash.LogUtil;
import com.ciban.smarthome.module.ui.base.BaseActivity;
import com.ciban.smarthome.module.ui.base.BaseFragment;
import com.ciban.smarthome.module.utils.ThreadManger;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends BaseActivity {


    private FrameLayout fl_content;
    private RadioGroup rg_menu;
    private RadioButton rb_area, rb_model, rb_setting;
    private LinearLayout ll_del;
    private Button bt_del, bt_delcancle;

    private AreraFragment areraFragment;
    private SceneFragment moduleFrament;
    private SettingFragment settingFragment;

    private BaseFragment currentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();

        rb_area.setChecked(true);

        checkPingIp();
    }

    private void checkPingIp() {
        ThreadManger.getInstance().execute(new Runnable() {

            @Override
            public void run() {
                boolean result = AppUtils.isPingIp("192.168.1.250");
                if (!result) {
                    LogUtil.info("当前手机网络和主机不在同一个网段内");
                    handler.sendEmptyMessage(HANDLER_PING_DISCONN);
                } else {
                    LogUtil.info("当前手机网络和主机在同一个网段内");
                    handler.sendEmptyMessage(HANDLER_PING_CONN);
                }
            }
        });
    }

    private void findViews() {
        fl_content = (FrameLayout) findViewById(R.id.main_fl_content);
        rg_menu = (RadioGroup) findViewById(R.id.main_rg_menu);
        rb_area = (RadioButton) findViewById(R.id.main_rb_area);
        rb_model = (RadioButton) findViewById(R.id.main_rb_module);
        rb_setting = (RadioButton) findViewById(R.id.main_rb_setting);
        ll_del = (LinearLayout) findViewById(R.id.main_del_content);
        bt_del = (Button) findViewById(R.id.main_del_bt);
        bt_delcancle = (Button) findViewById(R.id.main_del_bt_cancle);

        setListeners();
    }

    public void showDelView() {
        ll_del.setVisibility(View.VISIBLE);
    }

    private void setListeners() {
        rg_menu.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.main_rb_area:
                        if (areraFragment == null)
                            areraFragment = new AreraFragment();
                        getFragmentManager().beginTransaction()
                                .replace(R.id.main_fl_content, areraFragment)
                                .commit();
                        currentFragment = areraFragment;
                        moduleFrament = null;
                        settingFragment = null;
                        break;
                    case R.id.main_rb_module:
                        if (moduleFrament == null)
                            moduleFrament = new SceneFragment();
                        getFragmentManager().beginTransaction()
                                .replace(R.id.main_fl_content, moduleFrament)
                                .commit();
                        currentFragment = moduleFrament;
                        areraFragment = null;
                        settingFragment = null;
                        break;
                    case R.id.main_rb_setting:
                        if (settingFragment == null)
                            settingFragment = new SettingFragment();
                        getFragmentManager().beginTransaction()
                                .replace(R.id.main_fl_content, settingFragment)
                                .commit();
                        currentFragment = settingFragment;
                        moduleFrament = null;
                        areraFragment = null;
                        break;
                }
            }
        });

        bt_del.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (currentFragment.delItem()) {
                    ll_del.setVisibility(View.GONE);
                } else {
                }
            }
        });
        bt_delcancle.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                currentFragment.delCancle();
                ll_del.setVisibility(View.GONE);
            }
        });
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - exitTime < 2000) {
                finish();
                AppUtils.exitApp();
            } else {
                exitTime = System.currentTimeMillis();
                displayShort("再按一次退出应用程序");
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
